//
//  SkillTrainingInfo.m
//  EVE Mac Suite
//
//  Created by Vadim Lozko on 3/14/09.
//  Copyright 2009 __Vadim__CompanyName__. All rights reserved.
//

#import "EMSSkillTrainingInfo.h"
#import "EMSSkillPlannerTreeNode.h"
#import "EVECDSKill.h"
#import "EVECDLearnedGroup.h"
#import "EVECDLearnedSkill.h"
#import "EVECDQueueItem.h"
#import "EVECDCharacter.h"
#import "EVECDSkillRequirement.h"
#import "EVEAPICoreDataController.h"

@implementation EMSSkillTrainingInfo

static int skillPointsPerLevelForRank1[6] = {0, 250, 1414, 8000, 45255, 256000};

+(int)skillPointsRequiredForLevel:(int)level rank:(int)rank
{	
	return skillPointsPerLevelForRank1[level] * rank;
}

+(int)percentCompletedForCurrentPoints:(int)currPoints level:(int)level rank:(int)rank
{	
	if(level == 5)
	{
		return 100;
	} else {
		int adjustedBase = skillPointsPerLevelForRank1[level] * rank;
		int adjustedNextLevel = skillPointsPerLevelForRank1[level + 1] * rank - adjustedBase;
		int adjustedSkillPoints = currPoints - adjustedBase;
		return (int)((double)adjustedSkillPoints/adjustedNextLevel * 100);
	}
	//Shouldn't get to this point...
	return 0;
}

+(int)levelForSkillPoints:(int)points rank:(int)rank
{
	if(points == 0)
	{
		return 0;
	}
	int i;
	for(i = 0; i < 7; i++)
	{
		if(i == 6)
		{
			return 5;
		}
		if((skillPointsPerLevelForRank1[i] * rank) <= points && (skillPointsPerLevelForRank1[i + 1] * rank > points))
		{
			return i;
		}
	}
	return 0;
}



+(NSTimeInterval)trainingTimeToNextLevelForLearnedSkill:(EVECDLearnedSkill*)aSkill
{
	
	int skillPoints = aSkill.skillPointsLearned.intValue;
	int rank = aSkill.skill.rank.intValue;
	int currentLevel = aSkill.level.intValue;
	
	int skillPointsToNextLevel = [self skillPointsRequiredForLevel:(currentLevel + 1) rank:rank] - skillPoints;
	
	EVECDCharacter* character = aSkill.partOfLearnedGroup.learnedBy;
	
	double primaryAttributeValue = [[character valueForKey:[aSkill.skill valueForKey:@"primaryAttribute"]] doubleValue];
	double secondaryAttributeValue = [[character valueForKey:[aSkill.skill valueForKey:@"secondaryAttribute"]] doubleValue];
	
	NSTimeInterval seconds = (skillPointsToNextLevel/(primaryAttributeValue + (secondaryAttributeValue/2))) * 60;

	return seconds;
}

+(NSTimeInterval)trainingTimeToLevel:(NSInteger)aLevel forSkill:(EVECDLearnedSkill*)aSkill
{
	int skillPoints = aSkill.skillPointsLearned.intValue;
	int rank = aSkill.skill.rank.intValue;
	EVECDCharacter* character = aSkill.partOfLearnedGroup.learnedBy;
	
	int skillPointsToNextLevel = [EMSSkillTrainingInfo skillPointsRequiredForLevel:aLevel rank:rank] - skillPoints;
	
	double primaryAttributeValue = [[character valueForKey:[aSkill.skill valueForKey:@"primaryAttribute"]] doubleValue];
	double secondaryAttributeValue = [[character valueForKey:[aSkill.skill valueForKey:@"secondaryAttribute"]] doubleValue];
	
	NSTimeInterval seconds = (skillPointsToNextLevel/(primaryAttributeValue + (secondaryAttributeValue/2))) * 60;
	
	return seconds;
}

+(NSTimeInterval)trainingTimeForLevel:(NSInteger)aLevel forLearnedSkill:(EVECDLearnedSkill*)aSkill
{
	int skillPoints = aSkill.skillPointsLearned.intValue;
	int level = aSkill.level.intValue;
	int rank = aSkill.skill.rank.intValue;
	int minimumPointsForPreviousLevel = [EMSSkillTrainingInfo skillPointsRequiredForLevel:(level - 1) rank:rank];
	if(skillPoints < minimumPointsForPreviousLevel)
	{
		skillPoints = minimumPointsForPreviousLevel;
	}
	EVECDCharacter* character = aSkill.partOfLearnedGroup.learnedBy;
	
	int skillPointsToNextLevel = [EMSSkillTrainingInfo skillPointsRequiredForLevel:aLevel rank:rank] - skillPoints;
	
	double primaryAttributeValue = [[character valueForKey:[aSkill.skill valueForKey:@"primaryAttribute"]] doubleValue];
	double secondaryAttributeValue = [[character valueForKey:[aSkill.skill valueForKey:@"secondaryAttribute"]] doubleValue];
	
	NSTimeInterval seconds = (skillPointsToNextLevel/(primaryAttributeValue + (secondaryAttributeValue/2))) * 60;
	
	return seconds;
}


+(double)skillPointsPerSecondForSkill:(EVECDLearnedSkill*)aSkill
{
	NSString* primaryAttribute = aSkill.skill.primaryAttribute;
	NSString* secondaryAttribute = aSkill.skill.secondaryAttribute;
	EVECDCharacter* character = aSkill.partOfLearnedGroup.learnedBy;
	double primaryAttributeValue = [[character valueForKey:primaryAttribute] doubleValue];
	double secondaryAttributeValue = [[character valueForKey:secondaryAttribute] doubleValue];
	return (primaryAttributeValue + (secondaryAttributeValue/2)) / 60;
}

+(NSString*)romanNumeralFrontInt:(int)number
{
	switch (number) {
		case 1:
			return @"I";
		case 2:
			return @"II";
		case 3:
			return @"III";
		case 4:
			return @"IV";
		case 5:
			return @"V";
		default:
			return nil;
			break;
	}
}

+(HumanNotationTime)humanNotationTimeFromTimeInterval:(NSTimeInterval)totalSeconds
{
	HumanNotationTime time;
	int timeCounter = (int)totalSeconds;
	time.days = timeCounter / 86400;
	timeCounter %= 86400;
	time.hours = timeCounter / 3600;
	timeCounter %= 3600;
	time.minutes = timeCounter / 60;
	timeCounter %= 60;
	time.seconds = timeCounter;
	return time;
}


+(EMSSkillPlannerTreeNode*)generateTreeForSkill:(EVECDLearnedSkill*)aSkill level:(int)aLevel
{
	EMSSkillPlannerTreeNode* parentNode = [EMSSkillPlannerTreeNode treeNodeWithLearnedSkill:aSkill level:aLevel];
	EVECDCharacter* character = aSkill.partOfLearnedGroup.learnedBy;
	NSSet* learnedSkills = [EVECDContext fetchObjectsForEntityName:@"EVECDLearnedSkill" 
													 withPredicate:@"partOfLearnedGroup.learnedBy.name like %@", character.name];
	for(EVECDSkillRequirement* skillRequirement in aSkill.skill.skillRequirements)
	{
		EVECDSkill* requiredSkill = skillRequirement.skill;
		NSSet* filteredSet = [learnedSkills filteredSetUsingPredicate:[NSPredicate predicateWithFormat:@"skill.name like %@", requiredSkill.name]];
		EVECDLearnedSkill* requiredLearnedSkill = [filteredSet anyObject]; //Should only be one.
		[[parentNode mutableChildNodes] addObject:[self generateTreeForSkill:requiredLearnedSkill level:skillRequirement.level.intValue]];
	}
	return parentNode;
}

@end
